﻿namespace com.crownpack.Extensions.SharePointExtensions
{
    using System;
    using System.Collections.Generic;
    using System.Collections.Specialized;

    using Microsoft.SharePoint;

    public static class SPListExtensions
    {
        #region Methods

        /// <summary>
        /// Deletes all list items and folders in the specified list.
        /// </summary>
        /// <param name="spList"></param>
        public static void Clear( this SPList spList )
        {
            spList.ClearListItems( );
            spList.ClearFolders( );
        }

        /// <summary>
        /// Deletes all the folders in the specified list.
        /// </summary>
        /// <param name="spList"></param>
        public static void ClearFolders( this SPList spList )
        {
            List<int> ids = new List<int>( );
            foreach ( SPListItem spListItem in spList.Folders )
                ids.Add( spListItem.ID );
            foreach ( int id in ids )
                spList.Items.DeleteItemById( id );
        }

        /// <summary>
        /// Deletes all list items in the specified list.
        /// </summary>
        /// <param name="spList"></param>
        public static void ClearListItems( this SPList spList )
        {
            List<int> ids = new List<int>( );
            foreach ( SPListItem spListItem in spList.Items )
                ids.Add( spListItem.ID );
            foreach ( int id in ids )
                spList.Items.DeleteItemById( id );
        }

        public static SPView CreateView( this SPList spList, string viewTitle, List<string> viewFields, string query, uint rowLimit, bool paged, bool makeViewDefault, SPViewCollection.SPViewType viewType, bool replaceExisting )
        {
            return spList.CreateView( viewTitle, viewTitle, viewFields, query, rowLimit, paged, makeViewDefault, viewType, replaceExisting );
        }

        public static SPView CreateView( this SPList spList, string rootUrlName, string viewTitle, uint rowLimit, bool paged, bool makeViewDefault, SPViewCollection.SPViewType viewType, string viewSchema, bool replaceExisting )
        {
            SPView spView = spList.CreateView( rootUrlName, viewTitle, new List<string>( ), string.Empty, rowLimit, paged, makeViewDefault, viewType, replaceExisting );

            spView.SetViewSchemaXml( viewSchema );
            spView.Update( );
            return spView;
        }

        public static SPView CreateView( this SPList spList, string rootUrlName, string viewTitle, uint rowLimit, bool paged, bool makeViewDefault, SPViewCollection.SPViewType viewType, bool replaceExisting )
        {
            return spList.CreateView( rootUrlName, viewTitle, new List<string>( ), string.Empty, rowLimit, paged, makeViewDefault, viewType, replaceExisting );
        }

        public static SPView CreateView( this SPList spList, string rootUrlName, string viewTitle, List<string> viewFields, string query, uint rowLimit, bool paged, bool makeViewDefault, SPViewCollection.SPViewType viewType, bool replaceExisting )
        {
            SPView v = null;
            foreach ( SPView spView in spList.Views )
            {
                if ( spView.ToString( ).Equals( viewTitle, StringComparison.OrdinalIgnoreCase ) )
                {
                    v = spView;
                    break;
                }
            }
            if ( v != null && replaceExisting )
                spList.Views.Delete( v.ID );
            else if ( v != null && !replaceExisting )
                return v;

            StringCollection vf = new StringCollection( );
            vf.AddRange( viewFields.ToArray( ) );
            v = spList.Views.Add( rootUrlName, vf, query, rowLimit, paged, makeViewDefault, viewType, false );
            v.Title = viewTitle;
            v.Aggregations = string.Empty;
            spList.Update( );
            return v;
        }

        #endregion Methods
    }
}